<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/12/29
 * Time: 11:27
 */

namespace system\core\route;



use system\core\config\Config;
use system\library\Psr4AutoloaderClass;

class Load extends Psr4AutoloaderClass
{


    public static $instance=null;

    /*
     * 预加载的命名空间
     * [$prefix , $base_dir ]
     *
     * */
    public static $namespacearr = [
        ['app','/app'],
        ['extend','/extend'],
        ['system','/system'],
    ];


    protected function __construct(){
        $this->load();
    }

    /*
     * 单例生成
     * */
    public static function getInstance(){
        if (empty(self::$instance) ){
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /*
     * 注册命名空间
     * 包括系统命名空间和ventor命名空间
     * 
     * */
    public function load(){

        $narr = Config::$configmsg;

        self::$namespacearr = array_merge(self::$namespacearr, $narr['namespacearr']);

        //自动加载
        $this->register();

        //注册系统命名空间
        foreach (self::$namespacearr as $namespace){
            $this->addNamespace($namespace[0], $namespace[1]);
        }
        require VENDOR_PATH.'autoload.php';

    }


    /**
     * If a file exists, require it from the file system.
     *
     * @param string $file The file to require.
     * @return bool True if the file exists, false if not.
     */
    protected function requireFile($file)
    {   $file = dirname(realpath(APP_PATH)).$file;
//        var_dump("1.".$file);
        if (file_exists($file)) {
//            var_dump("2.".$file);
            require_once $file;
            return true;
        }
        return false;
    }



}